package cn.sugarscat.util;

import cn.sugarscat.db.DB;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Module {
    public static boolean hasThis(int id, DB db, String sql) {
        boolean has;
        ResultSet res = db.executeQuery(sql, ""+id);
        try {
            has = res.next();
            res.close();
            db.closeDB();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return has;
    }

    // 将字符串转换成时间
    public static LocalDateTime stringToTime(String time) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH:mm:ss");
        return LocalDateTime.parse(time, formatter);
    }

    // 将时间转换成字符串
    public static String timeToString(LocalDateTime time) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH:mm:ss");
        return time.format(formatter);
    }

    // 判断输入的是否是正确的时间字符串
    public static boolean isCorrectTime(String input) {
        String pattern = "\\d{4}-\\d{2}-\\d{2}-\\d{2}:\\d{2}:\\d{2}";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(input);
        return  m.matches();
    }
}
